{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #训练计划 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #linked-list #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #链表 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: trainingPlan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #训练计划 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个头节点为 <code>head</code> 的链表用于记录一系列核心肌群训练项目编号，请查找并返回倒数第 <code>cnt</code> 个训练项目编号。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>head = [2,4,7,8], cnt = 1\n",
    "<strong>输出：</strong>8</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= head.length &lt;= 100</code></li>\n",
    "\t<li><code>0 &lt;= head[i] &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= cnt &lt;= head.length</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof](https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof](https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,4,7,8]\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        t1, t2 = head, head\n",
    "        for _ in range(cnt):\n",
    "            t1 = t1.next\n",
    "        while t1:\n",
    "            t1, t2 = t1.next, t2.next\n",
    "        return t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast = head\n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "        \n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            head = head.next\n",
    "        return head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former,latter = head,head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former = former.next\n",
    "            latter = latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        a=0\n",
    "        cur=head\n",
    "        while cur:\n",
    "            a+=1\n",
    "            cur=cur.next\n",
    "        b=a-cnt\n",
    "        while b:\n",
    "            head=head.next\n",
    "            b-=1\n",
    "        return head  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        if head is None:\n",
    "            return None\n",
    "\n",
    "        fast = slow = head\n",
    "        for i in range(cnt):\n",
    "            if fast is None:\n",
    "                return None\n",
    "            fast = fast.next\n",
    "\n",
    "        while fast is not None:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "\n",
    "        return slow\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:  \n",
    "        i = 1\n",
    "        node_i = head\n",
    "        node_j = head\n",
    "        while i < cnt:\n",
    "            node_i = node_i.next\n",
    "            i += 1\n",
    "        \n",
    "        while node_i.next != None:\n",
    "            node_j = node_j.next\n",
    "            node_i = node_i.next\n",
    "\n",
    "        return node_j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        cur = head\n",
    "        i = 0\n",
    "        while cur:\n",
    "            cur = cur.next\n",
    "            i += 1\n",
    "        cur = head\n",
    "        for i in range(i - cnt):\n",
    "            cur = cur.next\n",
    "        return cur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        if not head:return None\n",
    "        fast=head\n",
    "        for _ in range(cnt-1):\n",
    "            fast=fast.next\n",
    "        slow=head\n",
    "        while fast and fast.next:\n",
    "            slow,fast=slow.next,fast.next\n",
    "        return slow\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        # 这个东西只需要定义一个快慢节点就可以啦\n",
    "        dummy = ListNode\n",
    "        dummy.next = head\n",
    "        fast ,slow = dummy,dummy\n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        pre, cur = head, head\n",
    "        for i in range(0, cnt):\n",
    "            pre = pre.next\n",
    "        while pre:\n",
    "            pre = pre.next\n",
    "            cur = cur.next\n",
    "        return cur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former,latter = head,head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former,latter=former.next,latter.next\n",
    "        return latter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast = slow = head\n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            if not former:return\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former, latter = former.next, latter.next\n",
    "        return latter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            if not former: return \n",
    "            former = former.next\n",
    "        while former:\n",
    "            former, latter = former.next, latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        index_1, index_2, length, count = head, head, 0, 0\n",
    "        while index_1:\n",
    "            length += 1\n",
    "            index_1 = index_1.next\n",
    "        target = length - cnt\n",
    "        while count != target:\n",
    "            count += 1\n",
    "            index_2 = index_2.next\n",
    "        return index_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        p = head\n",
    "        for _ in range(cnt):\n",
    "            head = head.next\n",
    "        \n",
    "        while head:\n",
    "            p = p.next\n",
    "            head = head.next\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        first = head\n",
    "        second = head\n",
    "        for i in range(cnt):\n",
    "            first = first.next\n",
    "        while first:\n",
    "            first = first.next\n",
    "            second = second.next\n",
    "        return second\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        left=head\n",
    "        right=head\n",
    "        for _ in range(cnt):\n",
    "            right=right.next\n",
    "        while right:\n",
    "            right=right.next\n",
    "            left=left.next\n",
    "        return left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast=head\n",
    "        low=head\n",
    "        for i in range(cnt):\n",
    "            fast=fast.next\n",
    "        while fast:\n",
    "            fast=fast.next\n",
    "            low=low.next\n",
    "        return low"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        res = []\n",
    "        while head:\n",
    "            res.append(head)\n",
    "            head = head.next\n",
    "        if len(res) >= cnt:\n",
    "            #num = -cnt\n",
    "            return res[-cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        len=1\n",
    "        p=head\n",
    "        while p.next:\n",
    "            p=p.next\n",
    "            len+=1\n",
    "        value=len-cnt+1\n",
    "        len=1\n",
    "        p=head\n",
    "        if value==len:\n",
    "            return p\n",
    "        while p.next:\n",
    "            p=p.next\n",
    "            len+=1\n",
    "            if len==value:\n",
    "                return p\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "\n",
    "        for _ in range(cnt):\n",
    "            if not former: return\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former, latter = former.next, latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        list = []\n",
    "        while head:\n",
    "            list.append(head)\n",
    "            head = head.next\n",
    "        return list[-cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former,latter=head,head\n",
    "        for _ in range(cnt):\n",
    "            former=former.next\n",
    "        while former:\n",
    "            former,latter=former.next,latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        count = 0\n",
    "        n = 1\n",
    "        last = head\n",
    "        while last:\n",
    "            count+=1\n",
    "            last = last.next\n",
    "\n",
    "        while head and n <(count - cnt +1):\n",
    "            head = head.next\n",
    "            n+=1\n",
    "        return head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        pre,tail=head,head\n",
    "        for _ in range(cnt):\n",
    "            pre=pre.next\n",
    "        while pre:\n",
    "            pre,tail=pre.next,tail.next\n",
    "        return tail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        length, num = 0, 0\n",
    "        point, result = head, head\n",
    "        for i in range(cnt):\n",
    "            point = point.next\n",
    "        while point:\n",
    "            point, result = point.next, result.next\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    #用快慢指针做\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            latter = latter.next\n",
    "            former = former.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        list = []\n",
    "        while head:\n",
    "            list.append(head)\n",
    "            head = head.next\n",
    "        return list[-cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        t1, t2 = head, head\n",
    "        for _ in range(cnt):\n",
    "            t1 = t1.next\n",
    "        while t1:\n",
    "            t1, t2 = t1.next, t2.next\n",
    "        return t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast = head\n",
    "        for i in range(cnt):\n",
    "            if fast:\n",
    "                fast = fast.next\n",
    "            else:\n",
    "                return None  # 如果链表长度小于 cnt，直接返回 None\n",
    "\n",
    "        slow = head\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "\n",
    "        return slow  # 返回倒数第 cnt 个节点的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former,latter = head,head \n",
    "        for _ in range(cnt):\n",
    "            former = former.next \n",
    "        while   former:\n",
    "            former, latter = former.next,latter.next \n",
    "        return latter\n",
    "\n",
    "\n",
    "# class Solution:\n",
    "#     def trainingPlan(self, head: ListNode, cnt: int) -> ListNode:\n",
    "#         former, latter = head, head\n",
    "#         for _ in range(cnt):\n",
    "#             former = former.next\n",
    "#         while former:\n",
    "#             former, latter = former.next, latter.next\n",
    "#         return latter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former, latter = former.next, latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        h = head\n",
    "        for _ in range(cnt):\n",
    "            head = head.next\n",
    "        while head:\n",
    "            h = h.next\n",
    "            head = head.next\n",
    "        return h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        count = 0\n",
    "        cur = head\n",
    "        while cur:\n",
    "            count+=1\n",
    "            cur = cur.next\n",
    "        cur = head\n",
    "        for i in range(0,count):\n",
    "            if i == count-cnt:\n",
    "                break\n",
    "            cur = cur.next\n",
    "        return cur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "# class Solution:\n",
    "#     def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "#         former,latter,k = head,head,0\n",
    "#         while k<cnt:\n",
    "#             former = former.next\n",
    "#             k+=1\n",
    "#         while former:\n",
    "#             former=former.next\n",
    "#             latter=latter.next\n",
    "#         return latter\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        if not head:\n",
    "            return None\n",
    "        \n",
    "        slow,fast = head,head\n",
    "        \n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "            \n",
    "        while fast:\n",
    "            slow = slow.next\n",
    "            fast = fast.next\n",
    "        \n",
    "        return slow       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        # 这个东西只需要定义一个快慢节点就可以啦\n",
    "        dummy = ListNode\n",
    "        dummy.next = head\n",
    "        fast ,slow = dummy,dummy\n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        \n",
    "        s, f = head, head\n",
    "        for _ in range(cnt):\n",
    "            f = f.next\n",
    "        while f:\n",
    "            f = f.next\n",
    "            s = s.next\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        from collections import deque\n",
    "        \n",
    "        window = deque(maxlen=cnt)\n",
    "        while head is not None:\n",
    "            window.append(head)\n",
    "            head = head.next\n",
    "        while cnt > 1:\n",
    "            window.pop()\n",
    "            cnt -= 1\n",
    "        return window.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast = slow = head \n",
    "        for _ in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next \n",
    "        return slow "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        arr = []\n",
    "        while head:\n",
    "            arr.append(head)\n",
    "            head = head.next\n",
    "        return arr[-cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        left, right = head, head\n",
    "        for _ in range(cnt):\n",
    "            right = right.next\n",
    "        \n",
    "        while right:\n",
    "            left = left.next\n",
    "            right = right.next\n",
    "        return left\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        tmp = head\n",
    "        for i in range(cnt-1):\n",
    "            tmp = tmp.next\n",
    "        pre = head\n",
    "        tmp = tmp.next\n",
    "        while tmp:\n",
    "            tmp = tmp.next\n",
    "            pre = pre.next\n",
    "        return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: ListNode, cnt: int) -> ListNode:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former, latter = former.next, latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        i = head\n",
    "        j = head\n",
    "        for k in range(cnt-1):\n",
    "            j = j.next\n",
    "        while j.next != None:\n",
    "            j = j.next\n",
    "            i = i.next\n",
    "        return(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        pre,res=head,head\n",
    "        while cnt:\n",
    "            pre=pre.next\n",
    "            cnt-=1\n",
    "        while pre:\n",
    "            pre=pre.next\n",
    "            res=res.next\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        slow, fast = head,head\n",
    "        for i in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        cur = head\n",
    "        i = 0\n",
    "        while cur:\n",
    "            cur = cur.next\n",
    "            i = i + 1\n",
    "        cur = head\n",
    "        for j in range(0, i-cnt):\n",
    "            cur = cur.next\n",
    "        return cur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        k = cnt\n",
    "        p1 = head\n",
    "        # p1 先走 k 步\n",
    "        for i in range(k):\n",
    "            p1 = p1.next\n",
    "        \n",
    "        p2 = head\n",
    "        # p1 和 p2 同时走 n - k 步\n",
    "        while p1:\n",
    "            p1 = p1.next\n",
    "            p2 = p2.next\n",
    "        \n",
    "         # p2 现在指向第 n - k + 1 个节点，即倒数第 k 个节点\n",
    "        return p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        Q = P = S = ListNode(0,head)\n",
    "        for i in range(0,cnt):\n",
    "            S = S.next\n",
    "        while S.next:\n",
    "            P = P.next\n",
    "            S = S.next\n",
    "        return P.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        p=head\n",
    "        q=head\n",
    "        while cnt-1>0:\n",
    "            q=q.next\n",
    "            cnt-=1\n",
    "        while q.next:\n",
    "            p=p.next\n",
    "            q=q.next\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        count = 0\n",
    "        temp = head\n",
    "        while temp:\n",
    "            count += 1\n",
    "            temp = temp.next\n",
    "        \n",
    "        while count > cnt:\n",
    "            head = head.next\n",
    "            count -= 1\n",
    "\n",
    "        return head\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        dummynode = ListNode(next = head)\n",
    "        fast = dummynode\n",
    "        while cnt > 0:\n",
    "            fast = fast.next\n",
    "            cnt -= 1\n",
    "        slow = dummynode\n",
    "        while fast :\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        fast = slow = head\n",
    "        for _ in range(cnt):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        first = head\n",
    "        latter = head\n",
    "        for _ in range(cnt):\n",
    "            first = first.next\n",
    "        while first != None:\n",
    "            first = first.next\n",
    "            latter = latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        first = head\n",
    "        last = head\n",
    "\n",
    "        while cnt and last:\n",
    "\n",
    "            last = last.next\n",
    "            cnt -= 1\n",
    "        while last:\n",
    "            first = first.next\n",
    "            last = last.next\n",
    "        return first\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        a = 0\n",
    "        h = head\n",
    "        while head:\n",
    "            a += 1\n",
    "            if a > cnt:\n",
    "                h = h.next\n",
    "            head = head.next\n",
    "        return h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        p,q=head,head\n",
    "        for i in range(cnt):\n",
    "            q=q.next\n",
    "        while q:\n",
    "            p=p.next\n",
    "            q=q.next\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        f,l = head,head\n",
    "        while (cnt-1):\n",
    "            f = f.next\n",
    "            cnt -= 1\n",
    "        while f.next:\n",
    "            l = l.next\n",
    "            f = f.next\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        if head is None:\n",
    "            return\n",
    "        cur = head\n",
    "        count = 0\n",
    "        while cur:\n",
    "            cur = cur.next\n",
    "            count += 1\n",
    "        index = count - cnt\n",
    "        i = 0\n",
    "        while head:\n",
    "            if i == index:\n",
    "                return head\n",
    "            else:\n",
    "                head = head.next\n",
    "                i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "#链表内存空间是不连续的，所以没有索引的概念\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former = head ; latter = head \n",
    "        for _  in range(cnt):#下划线表示占位符，循环制定cnt次数，但是不关心变量，不使用这个变量。\n",
    "            former = former.next\n",
    "        while former != None:\n",
    "            former = former.next ; latter = latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former,latter=head,head\n",
    "        while cnt>0:\n",
    "            former=former.next\n",
    "            cnt-=1\n",
    "        while former!=None:\n",
    "            former,latter=former.next,latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        h = head\n",
    "        for _ in range(cnt):\n",
    "            head = head.next\n",
    "        while head:\n",
    "            h = h.next\n",
    "            head = head.next\n",
    "        return h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        root = head\n",
    "        total = 0\n",
    "        while root:\n",
    "            total += 1\n",
    "            root = root.next\n",
    "        num = total - cnt\n",
    "        while head:\n",
    "            if num == 0:\n",
    "                return head\n",
    "            head = head.next\n",
    "            num -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "\n",
    "        i, slow, fast = 1, ListNode(0,head), head \n",
    "        while fast:\n",
    "            fast = fast.next \n",
    "            i += 1\n",
    "            if i > cnt:\n",
    "                slow = slow.next \n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        Q = P = S = ListNode(0,head)\n",
    "        for i in range(0,cnt):\n",
    "            S = S.next\n",
    "        while S.next:\n",
    "            P = P.next\n",
    "            S = S.next\n",
    "        return P.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        self.cnt = cnt\n",
    "        self.returnV = 0\n",
    "        def recur(n1,n2):\n",
    "            if n2:\n",
    "                returnNode = recur(n2,n2.next)\n",
    "                self.cnt-=1\n",
    "                if self.cnt==0:\n",
    "                    self.returnV = n2\n",
    "                return\n",
    "            else:\n",
    "                return\n",
    "        recur(None, head)\n",
    "        return self.returnV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        later, former = head, head\n",
    "        for i in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            later, former = later.next, former.next\n",
    "        return later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        former, latter = head, head\n",
    "        for _ in range(cnt):\n",
    "            former = former.next\n",
    "        while former:\n",
    "            former = former.next\n",
    "            latter = latter.next\n",
    "        return latter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        #快慢双指针\n",
    "        slow, fast = head, head\n",
    "        while cnt:\n",
    "            fast = fast.next\n",
    "            cnt -= 1\n",
    "        \n",
    "        while fast:\n",
    "            slow = slow.next\n",
    "            fast = fast.next\n",
    "        \n",
    "        return slow\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:\n",
    "        cur, hlen = head, 0\n",
    "        while cur:\n",
    "            hlen+=1\n",
    "            cur = cur.next\n",
    "        k = hlen - cnt\n",
    "        while k:\n",
    "            head = head.next\n",
    "            k-=1\n",
    "        return head"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
